﻿package com.lbynet.media{
	
	import flash.events.*;
    import flash.media.*;
    import flash.net.*;
	/**
	 * ...
	 * @author lbynet (Tools -> Custom Arguments...)
	 */
    public class SoundControl {
		
		private static var Instance:SoundControl = new SoundControl();
		
        private var musicFactory:Sound;
        private var musicChannel:SoundChannel;
		
		private var soundFactory:Sound;
        private var soundChannel:SoundChannel;
		
        public function SoundControl()
        {
           if(Instance!=null){
				throw new Error("单件")
			}
        }
		
		/*
		 * 单件类的实例 
		*/
		public static function getInstance():SoundControl{
			return Instance;
		}
		
		
		
        public function loadMusic(path:String) : void
        {
            var _urlRequest:URLRequest = new URLRequest(path);
            this.musicFactory = new Sound();
            this.musicFactory.addEventListener(Event.COMPLETE, this.completeMusicHandler);
            this.musicFactory.addEventListener(IOErrorEvent.IO_ERROR, this.ioErrorHandler);
            this.musicFactory.load(_urlRequest);
        }
		/*
		 * 加载完后，自动播放背景音乐 
		*/
		public function completeMusicHandler(event:Event) : void
        {
            this.musicChannel = this.musicFactory.play(0, 10000);
        }
		public function ioErrorHandler(event:Event) : void
        {
            trace("IOErrorEvent");
        }
        public function stopMusic() : void
        {
            if (this.musicChannel)
            {
                this.musicChannel.stop();
            }
        }
		public function playMusic() : void
        {
            this.musicChannel = this.musicFactory.play(0, 10000);
        }
		
		
		
		
		
		public function loadSound(path:String) : void
        {
            var _urlRequest:URLRequest = new URLRequest(path);
            this.soundFactory = new Sound();
            this.soundFactory.addEventListener(Event.COMPLETE, this.completeSoundHandler);
            this.soundFactory.addEventListener(IOErrorEvent.IO_ERROR, this.ioErrorHandler);
            this.soundFactory.load(_urlRequest);
        }
		public function playSound() : void
        {
            this.soundFactory.play(0, 1);
        }
		public function stopSound() : void
        {
            if (this.soundChannel)
            {
                this.soundChannel.stop();
            }
        }
        public function completeSoundHandler(event:Event) : void
        {
            this.soundChannel = this.soundFactory.play(0, 1);
        }
		
       
		
		/*
		 * 清除静态属性 
		*/
        public function setStaticObjectNull() : void
        {
            if (Instance) Instance = null;
        }
		
        
		
		
		
	}
	
}